<template>
  <div class="container">
    <el-card>
      <div id="button">
        <el-row type="flex" justify="space-between">
          <el-form :inline="true">
            <!-- 目录名称 -->
            <el-form-item label="目录名称">
              <el-input v-model="data.directoryName"></el-input>
            </el-form-item>
            <!-- 下拉框 -->
            <el-form-item label="状态">
              <el-select placeholder="请选择" v-model="data.state">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="Remove" size="small">清除</el-button>
              <el-button @click="Search" type="primary" size="small"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
          <!-- 新增目录 -->
          <div>
            <el-button
              @click="showDialogs"
              type="success"
              icon="el-icon-edit"
              size="small"
              style="margin-top: 7px"
              >新增目录</el-button
            >
          </div>
        </el-row>

        <el-alert
          :closable="false"
          :title="`数据一共 ${total} 条`"
          type="info"
          show-icon
        >
        </el-alert>
      </div>

      <!-- 表格 -->
      <el-table :data="items">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column
          width="100px"
          prop="subjectName"
          label="所属学科"
        ></el-table-column>
        <el-table-column
          prop="directoryName"
          label="目录名称"
        ></el-table-column>
        <el-table-column prop="username" label="创建者"></el-table-column>
        <el-table-column label="创建日期">
          <template v-slot="{ row }">
            {{ formDate(row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="totals"
          width="100px"
          label="面试题数量"
        ></el-table-column>
        <el-table-column label="状态" width="50px">
          <template v-slot="scope">
            {{ scope.row.state === 1 ? "启用" : "禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-row type="flex" justify="space-around">
              <el-link
                @click="Dpen(scope.row.id)"
                v-if="scope.row.state === 0"
                type="primary"
                >启用</el-link
              >
              <el-link @click="Disable(scope.row.id)" v-else type="primary"
                >禁用</el-link
              >
              <el-link
                :disabled="scope.row.state === 1 ? true : false"
                type="primary"
                @click="hMod(scope.row.id, scope.row)"
                >修改</el-link
              >
              <el-link
                :disabled="scope.row.state === 1 ? true : false"
                type="primary"
                @click="hDel(scope.row.id)"
                >删除</el-link
              >
            </el-row>
          </template>
        </el-table-column>
      </el-table>

      <el-row type="flex" justify="end" style="margin-top: 20px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 20, 50]"
          :background="true"
          :page-size="list.pagesize"
          :current-page="list.page"
          :total="total"
          layout="total, prev, pager, next, sizes,jumper"
        ></el-pagination>
      </el-row>
    </el-card>
    <!-- 分页 -->

    <!-- 新增弹框 -->
    <el-dialog
      :visible.sync="showDialog"
      :title="showDialogRole ? '修改' : '添加'"
      @close="Down"
    >
      <el-form>
        <el-form-item label="所属学科" label-width="80px">
          <el-select
            v-model="addList.subjectID"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in scienceList"
              :key="item.id"
              :value="item.id"
              :label="item.subjectName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" label-width="80px">
          <el-input
            v-model="addList.directoryName"
            placeholder="请输入目录名称"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item>
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="Determine">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  list,
  remove,
  scienceList,
  scienceDetails,
  add,
  detail,
  update,
  changeState,
} from "@/api/hmmm/directorys";
// import subjects from '@/api/hmmm/subjects'
import { parseTime } from "@/utils";
export default {
  data() {
    return {
      list: {
        // 列表
        page: 1,
        pagesize: 10,
      },
      pid: {
        id: "",
      },
      stateData: {
        // 状态
        state: "",
      },
      items: [],
      data: {
        // 搜索
        directoryName: "",
        state: "",
      },
      total: 0, // 总条数
      showDialog: false,
      addList: {
        // 添加弹框
        subjectID: "",
        directoryName: "",
      },
      showDialogRole: false, // 判断是添加还是修改 false:添加
      scienceList: [],
    };
  },
  created() {
    this.getDirectorys();
  },
  methods: {
    // 获取表格数据
    async getDirectorys() {
      const res = await list(this.list);
      this.items = res.data.items;
      console.log(res);
      // 总条数
      this.total = res.data.counts;
    },
    // 清除
    Remove() {
      this.data.directoryName = "";
      this.data.state = "";
      this.page = 1;
      this.pagesize = 10;

      this.getDirectorys();
    },
    // 搜索
    async Search() {
      const res = await list({ ...this.list, ...this.data });
      this.items = res.data.items;
      console.log(res.data.items);
    },
    // 分页组件 每条数
    handleSizeChange(val) {
      this.list.pagesize = val;
      // 刷新列表
      this.getDirectorys();
    },
    // 分页组件 页数
    handleCurrentChange(val) {
      this.list.page = val;
      // 刷新列表
      this.getDirectorys();
    },
    // 获取学科列表
    async science() {
      const res = await scienceList();
      console.log("科学列表", res);
      this.scienceList = res.data.items;
    },
    // 新增弹框
    showDialogs() {
      // 显示弹框
      this.showDialog = true;
      // 判断弹框是添加还是修改 false为添加
      this.showDialogRole = false;
      // 调用获取列表函数
      this.science();
    },
    // 弹框确认
    Determine() {
      // 判断是否是添加
      if (this.showDialogRole) {
        console.log("修改");
        // 将数据发送到服务器 修改函数
        this.change();
      } else {
        console.log("添加");
        // 将数据发送到服务器 添加函数
        this.addDirectory();
      }
    },
    // 添加
    async addDirectory() {
      // 发送axiso
      const res = await add(this.addList);
      console.log("添加", res);
      // 提示 添加的边界处理
      if (res.status === 200) {
        this.total++;
        // this.list.page = Math.ceil(this.total / this.list.pagesize);
        this.$message.success("添加成功");
      } else {
        console.log(res);
        this.$message.danger("添加失败");
      }
      // 刷新
      this.getDirectorys();
      // 关闭弹框
      this.showDialog = false;
    },
    // 修改
    async change() {
      const res = await update({ ...this.addList, ...this.pid });
      console.log("修改", res);
      this.$message.success("修改成功!");
      // 刷新
      this.getDirectorys();
      // 关闭弹框
      this.showDialog = false;
    },
    // 修改弹框
    async hMod(id, npm) {
      console.log(npm);
      // 显示弹框
      this.showDialog = true;
      // 判断弹框是添加还是修改 true为修改
      this.showDialogRole = true;
      // 调用获取列表函数
      this.science();
      // 根据id 获取到目标详情
      this.pid.id = id;
      const res = await detail({ ...this.pid, ...this.list });
      console.log("目标详情", res);
      // 根据id 获取到所属学科
      const ress = await scienceDetails(res.data);
      console.log("所属学科", ress);
      // 数据回填
      this.addList.subjectID = res.data.subjectID;
      this.addList.directoryName = res.data.directoryName;
    },
    // 状态启用
    Dpen(id) {
      console.log("状态开启", id);
      this.pid.id = id;
      this.stateData.state = 1;
      // 调用请求
      this.stateRequest();
      // 刷新
      this.getDirectorys();
    },
    // 状态禁用
    Disable(id) {
      console.log("状态禁用", id);
      this.pid.id = id;
      this.stateData.state = 0;
      // 调用请求
      this.stateRequest();
      // 刷新
      this.getDirectorys();
    },
    // 状态请求
    async stateRequest() {
      const res = await changeState({ ...this.pid, ...this.stateData });
      console.log("状态请求", res);
      // 刷新
      this.getDirectorys();
    },
    // 删除
    async hDel(id) {
      console.log("id", id);
      this.pid.id = id;
      const res = await remove({ ...this.pid, ...this.list });
      console.log("删除", res);
      // 提示 删除的边界处理
      if (res.data.success) {
        if (this.total % this.list.pagesize === 1) {
          this.list.page >= 1 ? (this.list.page -= 1) : (this.list.page = 1);
        }
        this.$message.success("删除成功");
      } else {
        this.$message.danger("删除失败");
      }
      // 刷新
      this.getDirectorys();
    },
    // 弹框关闭
    Down() {
      this.addList.subjectID = "";
      this.addList.directoryName = "";
    },
    // 解析时间
    formDate(value) {
      return parseTime(value);
    },
  },
};
</script>

<style scoped >
</style>
